package longrise.phone.com.itssnew.utils;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.widget.Toast;

import java.net.NetworkInterface;
import java.net.SocketException;

import longrise.phone.com.itssnew.MyApplication;
import longrise.phone.com.itssnew.widget.AlertDialog;

/**
 * Created by Administrator on 2018/1/18.
 */

public class CommonUtil {
    public static Toast mToast1 = null;
    /** 得到上下文 */
    public static Context getContext()
    {
        return MyApplication.getAppContext();
    }

    /** 得到Resouce对象 */
    public static Resources getResource()
    {
        return getContext().getResources();
    }

    /** 得到String.xml中的字符串 */
    public static String getString(int resId)
    {
        return getResource().getString(resId);
    }

    /**
     * 显示toast
     *
     */
    public static void showToast(final Context context, String content)
    {
        if (mToast1 == null)
        {
            mToast1 = Toast.makeText(context, content, Toast.LENGTH_LONG);

        } else
        {
            mToast1.setText(content);
            mToast1.setDuration(Toast.LENGTH_SHORT);

        }

        mToast1.show();

    }

    /**
     * 确定取消提示对话框
     *
     * @param context
     * @param content
     * @param leftClick
     * @param rightClick
     */
    public static AlertDialog showDialog(Context context, String content, View.OnClickListener leftClick,
                                         View.OnClickListener rightClick) {
        return showDialog(context, "提示", content, "取消", "确定", leftClick, rightClick);
    }

    /**
     * 选择对话框
     *
     * @param context    上下文
     * @param title      标题
     * @param content    内容
     * @param leftStr    左边btn内容
     * @param rightStr   右边btn内容
     * @param leftClick
     * @param rightClick
     */
    public static AlertDialog showDialog(Context context, String title, String content, String leftStr, String rightStr,
                                         View.OnClickListener leftClick, View.OnClickListener rightClick) {
        final AlertDialog dialog = new AlertDialog(context).builder();
        dialog.setTitle(title);
        dialog.setShowTitle(true);
        dialog.setShowEditText(false);
        dialog.setMsg(content);
        dialog.setNegativeButton(leftStr, leftClick);
        dialog.setPositiveButton(rightStr, rightClick);
        dialog.show();
        return dialog;
    }

    /**
     * 空数据转化为“”
     * @return
     */
    public static String nullToString(String mCompanyBeanString){
        if(mCompanyBeanString==null) return "";
        else return mCompanyBeanString;
    }


    /**
     * 获取设备的mac
     * @return
     */
    public static String getLocalMacAddress() {
        String macAddress = null;
        StringBuffer buf = new StringBuffer();
        NetworkInterface networkInterface = null;
        try {
            networkInterface = NetworkInterface.getByName("eth1");
            if (networkInterface == null) {
                networkInterface = NetworkInterface.getByName("wlan0");
            }
            if (networkInterface == null) {
                return "02:00:00:00:00:02";
            }
            byte[] addr = networkInterface.getHardwareAddress();
            for (byte b : addr) {
                buf.append(String.format("%02X:", b));
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            macAddress = buf.toString();
        } catch (SocketException e) {
            e.printStackTrace();
            return "02:00:00:00:00:02";
        }
        return macAddress;
    }

}
